package mya.mes.mdc.logic.dispatch.executor;

import mya.common.jpa.JPAUtils;
import mya.common.jpa.context.NoLoginLogic;
import mya.common.jpa.context.ServiceContext;
import mya.mes.mdc.model.dispatch.MdcDispatchContent;
import mya.mes.mdc.queries.dispatch.MdcDispatchContentQuery;

import java.util.LinkedList;
import java.util.List;


class DispatchCurrent {

    private LinkedList<MdcDispatchContent> linkedQueue = new LinkedList();


    public synchronized void queueDispatch(MdcDispatchContent dispatchContent) {
        linkedQueue.offer(dispatchContent);
    }

    public synchronized MdcDispatchContent getDispatch() {
        return linkedQueue.poll();
    }

    public void updateCache() throws Exception {
        List<MdcDispatchContent> alarmList = loadCurrent();
        linkedQueue.clear();
        linkedQueue.addAll(alarmList);
    }


    public List<MdcDispatchContent> loadCurrent(ServiceContext context) throws Exception {
        List<MdcDispatchContent> tmp = MdcDispatchContentQuery.loadAll(context);
        return tmp;
    }


    public List<MdcDispatchContent> loadCurrent() throws Exception {
        NoLoginLogic logic = new NoLoginLogic(JPAUtils.DEFAULT_TENANT_ID) {
            @Override
            protected Object process(ServiceContext context) throws Exception {
                List<MdcDispatchContent> tmp = MdcDispatchContentQuery.loadAll(context);
                return tmp;
            }
        };
        return (List<MdcDispatchContent>) logic.callLogic();
    }


}
